import json
from gevent import pywsgi
from flask import request,Flask
import cv2
import numpy as np
import base64
 
# requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL'

app = Flask(__name__)
app.debug = True

@app.route("/", methods=['GET','POST'])
def get_frame():
    #start_time = time.time()
    upload_file = request.get_data()
    req = json.loads(upload_file)
    if upload_file:
        name = req['name']
        print(name)
        img_str = req['image'] #得到unicode的字符串
        img_decode_ = img_str.encode('ascii') #从unicode变成ascii编码
        img_decode = base64.b64decode(img_decode_) #解base64编码，得图片的二进制
        img_np_ = np.frombuffer(img_decode, np.uint8)
        img = cv2.imdecode(img_np_, cv2.COLOR_RGB2BGR) #转为opencv格式
        cv2.imshow('frame', img)
        
        cv2.waitKey(0)
        return 'success'
    else:
        return 'failed'

if __name__ == "__main__":
    app.config["SECRET_KEY"] = "ITSASECRET"
    http_server = pywsgi.WSGIServer(('127.0.0.1', 4443), app, keyfile='..\key.pem',certfile='..\cert.pem')
    http_server.serve_forever()